﻿using AbpApplication.AppServices;
using Flurl.Http;
using HandyControl.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf;
using NewWms.Extend;
using NewWms.Apiservice.InPut;
using NewWms.Apiservice.Dto;

namespace NewWms.Apiservice
{
    public class UserService : MyUserService
    {
        string config = GlobalVariable.Config;
        public async Task<bool> UpdateUserInfo(InPut_UserService4 inPut)
        {
            try
            {
                var url = $"http://{config}/api/User/UpdateUserInfo";
                var data = inPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog(" UpdateUserInfo", ex);
                return false;
            }
        }//更新用户信息
        public async Task<Tuple<UserDto, bool>> FindOneUserInfo(string UserName)
        {
            try
            {
                var url = $"http://{config}/api/User/FindOneUserInfo?UserName={UserName}";
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostAsync().ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    UserDto userDto = JsonConvert.DeserializeObject<UserDto>(Response.Dto.ToString());
                    return Tuple.Create(userDto, true);
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Tuple.Create<UserDto, bool>(null, false);
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("FindOneUserInfo", ex);
                return Tuple.Create<UserDto, bool>(null, false);
            }
        }//查询单一用户信息
        public async Task<bool> UpdateRole(InPut_UserService5 inPut)
        {
            try
            {
                var url = $"http://{config}/api/User/UpdateRole";
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(inPut).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State) Growl.Success(Response.Msg, "msg");
                else Growl.Info(Response.Msg, "msg");
                return Response.State;

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("UpdateRole", ex);
                return false;
            }
        }//更新角色
        public async Task<bool> UpdatePassWord(InPut_UserService3 inPut)
        {
            try
            {
                var url = $"http://{config}/api/User/UpdatePassWord";
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(inPut).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State) Growl.Success(Response.Msg, "msg");
                else Growl.Info(Response.Msg, "msg");
                return Response.State;

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("UpdatePassWord", ex);
                return false;
            }
        }//更新密码
        public async Task<bool> RegisterUser(InPut_UserService inPut)
        {
            try
            {
                var url = $"http://{config}/api/User/RegisterUser";
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(inPut).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State) Growl.Success(Response.Msg, "msg");
                else Growl.Info(Response.Msg, "msg");
                return Response.State;
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("RegisterUser", ex);
                return false;
            }
        }//注册用户
        public async Task<bool> LoginUser(InPut_UserService2 inPut)
        {
            try
            {
                var url = $"http://{config}/api/User/LoginUser";
                string result = await url.PostJsonAsync(inPut).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success("登录成功", "msg");
                    Jwt.JwtKey = Response.Dto.ToString();
                    Jwt.UserName = Response.Msg;
                    EventAggregatorHelper.EventAggregator.SendMessage(Jwt.UserName, "UserName");//"Setting" 给消息聚合器打上标记 
                }

                else MessageBox.Show(Response.Msg);
                return Response.State;
            }
            catch (Exception ex)
            {
                MessageBox.Show("接口调用失败");
                LognetHelper.WriteLog("RegisterUser", ex);
                return false;
            }
        }//登录
    }
}
